////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.EntityClasses;
using RPSDAL.FactoryClasses;
using RPSDAL.DaoClasses;
using RPSDAL.RelationClasses;
using RPSDAL.ValidatorClasses;
using RPSDAL.TypedViewClasses;
using RPSDAL.StoredProcedureCallerClasses;
using RPSDAL.HelperClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;



public partial class User : System.Web.UI.Page
{
    private bool isUpdate = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Parameters.LoggedUser != -1)
            {
                UserEntity eUser = new UserEntity(Parameters.LoggedUser);
                RoleEntity eRole = new RoleEntity(eUser.RoleId);

                if (!eRole.HasEditUser)
                {
                    Parameters.LoggedUser = -1;
                    Response.Redirect("Default.aspx");
                    return;
                }
            }



            lblUserExist.Visible = false;
            lblSteluta.Visible = false;
            LoadRoles();
            if (Request.QueryString["id"] != null)
            {
                UserEntity eUser = new UserEntity(Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));

                txtFirstName.Text = eUser.FirstName;
                txtLastName.Text = eUser.LastName;
                txtConnectName.Text = eUser.ConnectName;
                txtPassword.Attributes["value"] = eUser.Password;
                txtRepassword.Attributes["value"] = eUser.Password;
                cmbRoles.SelectedValue = eUser.RoleId.ToString();
                txtEmail.Text = eUser.Email;
                chkActive.Checked = eUser.Active;
                chkViewAllContacts.Checked = eUser.ViewAllContacts;
            }

        }
    }

    private bool IsUpdate 
    {
        get { return isUpdate; }
        set { isUpdate = value; }
    }

    #region methods

    protected void Save(object sender, EventArgs e)
    {
        try
        {
            Global.UserLogSave(Parameters.LoggedUser, "Save User");
            UserEntity eUser = new UserEntity();
            if (Page.IsPostBack && Request.QueryString["id"]!=null)
            {
                eUser = new UserEntity(Convert.ToInt32(Global.DecryptString(Request.QueryString["id"].ToString())));
            }


            int index = 0;
            if (eUser.IsNew || eUser.ConnectName != txtConnectName.Text)
            {
                UserCollection cUsers = new UserCollection();
                IPredicateExpression filter = new PredicateExpression();
                filter.Add(UserFields.ConnectName == txtConnectName.Text);
                index = cUsers.GetDbCount(filter);
            }

            eUser.FirstName = txtFirstName.Text;            
            eUser.LastName = txtLastName.Text;
            eUser.ConnectName = txtConnectName.Text;
            eUser.Password = txtPassword.Text;
            eUser.RoleId = Convert.ToInt32(cmbRoles.SelectedValue);
            eUser.Email = txtEmail.Text;
            eUser.ViewAllContacts = chkViewAllContacts.Checked;
            eUser.Active = chkActive.Checked;


            //bool bExist = false;
            //for (index = 0; index < cUsers.Count; index++)
            //{
            //    if (cUsers[index].ConnectName == txtConnectName.Text)
            //    {
            //        bExist = true;
            //        break;
            //    }
            //}
            if (index == 0)
            {
                eUser.Save();
                lblSavedUser.Visible = true;
                //Response.Redirect("UserList.aspx");
            }
            else
            {
                lblSteluta.Visible = true;
                lblUserExist.Visible = true;
            }
        }
        catch { };
    }
    protected void Cancel(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx");
    }  

    protected void LoadRoles()
    {
        RoleCollection cRoles = new RoleCollection();       
       
        cmbRoles.DataValueField = "RoleId";
        cmbRoles.DataTextField = "Name";
        cRoles.GetMulti(null);
        cmbRoles.DataSource = cRoles;
        cmbRoles.DataBind();     
    }

    #endregion methods
}

 

